//
//  SelectableButton.swift
//  SCA
//
//  Created by tjun on 2025/9/1.
//

import UIKit

class SelectableButton: UIButton {
    
    private let underlineView = UIView()
    
    // 配置选中和未选中的颜色
    var selectedColor: UIColor = Assets.Color.loginTypeTitleSelect {
        didSet {
            updateStyle()
        }
    }
    var normalColor: UIColor = Assets.Color.loginTypeTitleNormal {
        didSet {
            updateStyle()
        }
    }
    var underlineColor: UIColor = Assets.Color.white {
        didSet {
            underlineView.backgroundColor = underlineColor
        }
    }
    
    var selectedFont: UIFont = UIFontMediumMake(16) {
         didSet { updateStyle() }
     }
    
     var normalFont: UIFont = UIFontMake(16) {
         didSet { updateStyle() }
     }
    
    override var isSelected: Bool {
        didSet {
            updateStyle()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }
    
    private func setup() {
        setTitleColor(normalColor, for: .normal)
        setTitleColor(selectedColor, for: .selected)
        
        underlineView.backgroundColor = underlineColor
        underlineView.isHidden = true
        addSubview(underlineView)
        
        underlineView.snp.makeConstraints { make in
            make.height.equalTo(2)
            make.centerX.equalToSuperview()
            make.width.equalTo(22)
            make.bottom.equalToSuperview()
        }
        updateStyle()
    }
    
    private func updateStyle() {
        underlineView.isHidden = !isSelected
        setTitleColor(normalColor, for: .normal)
        setTitleColor(selectedColor, for: .selected)
        
        if isSelected {
            titleLabel?.font = selectedFont
        } else {
            titleLabel?.font = normalFont
        }
    }
}


class SelectableGradientLineButton: UIButton {
    
    private let underlineView = GradientView()
    
    // 配置选中和未选中的颜色
    var selectedColor: UIColor = .white {
        didSet {
            updateStyle()
        }
    }
    var normalColor: UIColor = .lightGray {
        didSet {
            updateStyle()
        }
    }
    var underlineColor: UIColor = .white {
        didSet {
            underlineView.backgroundColor = underlineColor
        }
    }
    
    override var isSelected: Bool {
        didSet {
            updateStyle()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }
    
    private func setup() {
        setTitleColor(normalColor, for: .normal)
        setTitleColor(selectedColor, for: .selected)
        
        underlineView.colors = [UIColorMake(0x6A68F5), UIColorMake(0xBE0DEF), UIColorMake(0xF202D3)]
        underlineView.locations = [0, 0.5, 1]
        underlineView.setStartPoint(CGPoint(x: 0, y: 0.5), endPoint: CGPointMake(1, 0.5))
        underlineView.isHidden = true
        addSubview(underlineView)
        
        underlineView.snp.makeConstraints { make in
            make.height.equalTo(2)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        updateStyle()
    }
    
    private func updateStyle() {
        underlineView.isHidden = !isSelected
    }
}
